<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function deepCopy(obj1) {
            var obj2 = Object.prototype.toString.call(obj1).slice(8, -1) === 'Array' ? [] : {};
            for (var key in obj1) {
                //分两种情况,
                //  1.简单数据类型直接传值
                //  2.复杂数据类型还要重新生成一个新地址
                if (obj1.hasOwnProperty(key)) {
                    if (typeof obj1[key] === 'object') {
                        //复杂数据类型,数组/对象
                        //这里用了递归
                        obj2[key] = deepCopy(obj1[key]);
                    } else {
                        obj2[key] = obj1[key];
                    }
                }
            }
            return obj2;
        }

        var obj1 = {
            name: ':·.',
            age: 21,
            hobby: {
                game: 'steam'
            },
        }

        var obj2 = deepCopy(obj1);
        obj2.name = '∵·∴'
        obj2.hobby.game = 'shteam/盐与避难所'
        console.log(obj1.name);
        console.log(obj1.hobby);
        console.log(obj2.name);
        console.log(obj2.hobby);

        //for in 循环对象:可以循环原型链
        //Object.hasOwnProperty(key):可以判断某个属性是否是对象自身属性
    
        //typeof        简单数据类型和函数(可以更安全的判断数据)
        //instanceof    判断实例和构造器
        //constructor可以判断数据类型:某个实例是否是某个构造器构造出来的
        var arr =[1,2,3]
        if(arr.constructor === Array){
            console.log('是数组');
        }else{
            console.log('不是');
        }

        //用json来深拷贝
        var obj3 = JSON.parse(JSON.stringify(obj1))
        console.log(obj3);
    </script>
</body>

</html>